import cv2 as cv

videoFileName = r'E:\images\vtest.avi'
feature_params = dict(maxCorners = 100,
                      qualityLevel = 0.3,
                      minDistance = 7,
                      blockSize =7)
lk_params = dict(
    winSize = (15,15),
    maxLevel = 2,
    criteria = (cv.TERM_CRITERIA_EPS | cv.TERM_CRITERIA_COUNT, 10, 0.03
))

cap = cv.VideoCapture(videoFileName)

ret , prev = cap.read()
prevGray = cv.cvtColor(prev, cv.COLOR_BGR2GRAY)
p0 = cv.goodFeaturesToTrack(prevGray, mask=None, **feature_params)
while True:
    ret, frame =cap.read()
    if not ret:
        break

    gray= cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
    p1, st, err = cv.calcOpticalFlowPyrLK(prevGray, gray, p0, None, **lk_params)
    goodPoints = p1[st==1]
    goodPrevPoints = p0[st==1]

    res = frame.copy()
    drawColor = (0, 0, 255)
    for i, (cur, prev) in enumerate(zip(goodPoints, goodPrevPoints)):
        x0, y0 = cur.ravel()
        x1, y1 = prev.ravel()
        cv.line(res, (x0, y0), (x1, y1), drawColor)
        cv.circle(res, (x0, y0), 3, drawColor)

    prevGray = gray.copy()
    p0 = goodPoints.reshape(-1,1,2)
    cv.imshow('检测结果', res)
    key = cv.waitKey(30)
    if key == 27:
        break